
import 'package:bookshelf/event/inventory_rfid_event.dart';
import 'package:bookshelf/main.dart';
import 'package:bookshelf/serial/send_helper.dart';

enum Mode { reader,readerBlock, identify,identifyBind, inventory,readBlock, resetToReady, eas, afi, auth,none }

class ThreeToOneManager{

  late ThreeToOneManager manager;

  ThreeToOneManager getManager(){
    return manager;
  }

  factory ThreeToOneManager() => _singleton;

  static final ThreeToOneManager _singleton = ThreeToOneManager._internal();

  ThreeToOneManager._internal();

  Mode? mode;
  bool isBorrow = false;
  String rfid = '';
  String? no = '';


  setMode(Mode mode){
    this.mode = mode;
  }

  setModeAndStart(Mode mode,{bool isBorrow = false}){
    this.mode = mode;
    this.isBorrow = isBorrow;
    if(mode == Mode.reader || mode == Mode.readerBlock){
      idSerialPortHelper.sendData(setIos1443A());
    }
    if(mode == Mode.identify || mode == Mode.identifyBind){
      idSerialPortHelper.sendData(setIos1443B());
    }
    if(mode == Mode.inventory){
      idSerialPortHelper.sendData(setIos15693());
    }
  }

  setModeSuccess(){
    if(mode == Mode.reader || mode == Mode.readerBlock){
      idSerialPortHelper.sendData(setRequest());
      return;
    }
    if(mode == Mode.identify || mode == Mode.identifyBind){
      idSerialPortHelper.sendData(readerIdentify());
      return;
    }
    if(mode == Mode.inventory){
      idSerialPortHelper.sendData(inventory());
      return;
    }
    if(mode == Mode.resetToReady){
      setMode(Mode.eas);
      if(isBorrow){
        // idSerialPortHelper.sendData(readBlock(rfid));
        idSerialPortHelper.sendData(setEasBorrow());
      }else{
        idSerialPortHelper.sendData(setEasReturn());
      }
      return;
    }
  }

  setRequestSuccess(){
    if(mode == Mode.reader || mode == Mode.readerBlock){
      idSerialPortHelper.sendData(readReader());
    }
  }

  setInventorySuccess(String rfid){
    this.rfid = rfid;
    setMode(Mode.readBlock);
    idSerialPortHelper.sendData(readBlock(rfid));
  }

  setNoSuccess(String? no){
    this.no = no;
    setMode(Mode.resetToReady);
    idSerialPortHelper.sendData(resetToReady(rfid));
  }

  setRequestFailed(){
    if(mode == Mode.reader || mode == Mode.readerBlock){
      idSerialPortHelper.sendData(setRequest());
    }
    if(mode == Mode.identify || mode == Mode.identifyBind){
      idSerialPortHelper.sendData(readerIdentify());
    }
    if(mode == Mode.inventory || mode == Mode.readBlock|| mode == Mode.resetToReady ||  mode == Mode.eas ||  mode == Mode.afi){
      idSerialPortHelper.sendData(inventory());
    }
  }

  setEasSuccess(){
    setMode(Mode.afi);
    if(isBorrow){
      idSerialPortHelper.sendData(setAfiBorrow(rfid));
    }else{
      idSerialPortHelper.sendData(setAfiReturn(rfid));
    }
  }

  setAfiSuccess(){
    eventFactory.fire(InventoryRfidEvent(rfid,no));
  }


  setSelect(uid){
    idSerialPortHelper.sendData(selectReader(uid));
  }

  setAuthKey(){
    setMode(Mode.auth);
    idSerialPortHelper.sendData(authKey());
  }

  setAuthSuccess(){
    setMode(Mode.readerBlock);
    setReaderBlock();
  }

  setAuthFail(){
    setMode(Mode.readerBlock);
    setRequestFailed();
  }

  setReaderBlock(){
    idSerialPortHelper.sendData(readReaderBlock());
  }

}